//
//  ADButton.m
//  ADKit
//
//  Created by duzexu on 2017/12/19.
//

#import "ADButton.h"
#import "ADKitHeaders.h"

@interface ADButton ()

@property (nonatomic, assign) CGSize intrinsicSize;

@end

@implementation ADButton

+ (instancetype)buttonWithSpace:(NSInteger)buttonSpace {
    return [self buttonWithLayout:ADButtonLayoutNormal space:buttonSpace];
}

+ (instancetype)buttonWithLayout:(ADButtonLayout)buttonLayout space:(NSInteger)buttonSpace {
    ADButton *btn = [ADButton buttonWithType:UIButtonTypeCustom];
    btn.layout = buttonLayout;
    btn.space = buttonSpace;
    return btn;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self _setResizableBackgroundImageForState:self.state];
    [self setLayout:self.layout spacing:self.space];
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state {
    [super setTitle:title forState:state];
    [self setLayout:self.layout spacing:self.space];
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state {
    [super setImage:image forState:state];
    [self setLayout:self.layout spacing:self.space];
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    [self _setResizableBackgroundImageForState:self.state];
}

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    [self _setResizableBackgroundImageForState:self.state];
}

- (void)_setResizableBackgroundImageForState:(UIControlState)state {
    if (!_resizable) {
        return;
    }
    UIImage *image = [self backgroundImageForState:state];
    [self setBackgroundImage:[image ad_resizableImage] forState:state];
}

@end
